package com.wlcj.www.model.product;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Model;
import com.wlcj.util.RedisUtil;
import com.wlcj.util.SqlUtil;

/**
 * 未完成标
 * @author kangrui
 *
 */
public class UnfinishedProduct extends Model<UnfinishedProduct> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5858084181603493733L;
	
	public static final UnfinishedProduct dao = new UnfinishedProduct();

	/**
	 * 最新标的
	 */
	public List<UnfinishedProduct> getNewProduct(){
		return find(SqlUtil.sql("product.getNewProduct"));
	}
	
	/**
	 * 人气产品和推荐产品和限时产品和新品
	 */
	public Map<String,List<UnfinishedProduct>> getGoodProduct(){
		Map<String,List<UnfinishedProduct>> maps = new HashMap<String, List<UnfinishedProduct>>();
		UnfinishedProduct ups =  UnfinishedProduct.dao.findFirst("select max(create_time) as date from wd_p_product_unfinished ");
		String sql1 = "select a.*,b.logo,c.platform_name from wd_p_product_unfinished a  left join wd_p_img b  on a.platform_id = b.platform_id,wd_p_platform c where 80<=borrow_progress and borrow_progress<=99 and a.platform_id=c.platform_id and a.create_time = ?  order by a.borrow_progress asc  limit 0,1  ";//人气
		String sql2 = "select a.*,b.logo,c.platform_name from wd_p_product_unfinished a  left join wd_p_img b  on a.platform_id = b.platform_id,wd_p_platform c where 10<=borrow_progress and borrow_progress<=60 and a.platform_id=c.platform_id and a.create_time = ?  order by a.borrow_progress asc  limit 0,1   ";//新品
		String sql3 = "select a.*,b.logo,c.platform_name from wd_p_product_unfinished a  left join wd_p_img b  on a.platform_id = b.platform_id,wd_p_platform c where 90<=borrow_progress and borrow_progress<=99 and a.platform_id=c.platform_id and a.create_time = ?  order by a.borrow_progress asc  limit 0,1";//限时
		String sql4 = "select a.*,b.logo,c.platform_name from wd_p_product_unfinished a  left join wd_p_img b  on a.platform_id = b.platform_id,wd_p_platform c where 60<=borrow_progress and borrow_progress<=99 and a.platform_id=c.platform_id and a.create_time = ?  order by a.borrow_progress asc  limit 0,1";//推荐
		
		String  sqlId = "select * from wd_p_product_unfinished  where platform_id = ? and p_product_id = ? and  create_time = ? ";
		//人气产品
		if(RedisUtil.get("renqibiaodi") == null ){ //第一遍
			List<UnfinishedProduct> one = find(sql1,ups.get("date"));
			maps.put("renqi", one);
			RedisUtil.set("renqibiaodi", (Serializable) one);
		}else{ 
			List<UnfinishedProduct> one = RedisUtil.get("renqibiaodi");
			if(find(sqlId,one.get(0).get("platform_id"),one.get(0).get("p_product_id"),one.get(0).get("create_time"))==null){//满标情况
				List<UnfinishedProduct> two = find(sql1,ups.get("date"));
				maps.put("renqi", two);
				RedisUtil.set("renqibiaodi", (Serializable) two);
			}else{
				maps.put("renqi", one);
			}
			
		}
		//新品
		//maps.put("news", find(sql2,ups.get("date")));
		if(RedisUtil.get("xinpinchanpin") == null ){ //第一遍
			List<UnfinishedProduct> one = find(sql2,ups.get("date"));
			maps.put("news", one);
			RedisUtil.set("xinpinchanpin", (Serializable) one);
		}else{ 
			List<UnfinishedProduct> one = RedisUtil.get("xinpinchanpin");
			if(find(sqlId,one.get(0).get("platform_id"),one.get(0).get("p_product_id"),one.get(0).get("create_time"))==null){//满标情况
				List<UnfinishedProduct> two = find(sql2,ups.get("date"));
				maps.put("news", two);
				RedisUtil.set("xinpinchanpin", (Serializable) two);
			}else{
				maps.put("news", one);
			}
			
		}
		
		
		
		//限时产品
		//maps.put("xianshi", find(sql3,ups.get("date")));
		
		if(RedisUtil.get("xianshichanpin") == null ){ //第一遍
			List<UnfinishedProduct> one = find(sql3,ups.get("date"));
			maps.put("xianshi", one);
			RedisUtil.set("xianshichanpin", (Serializable) one);
		}else{ 
			List<UnfinishedProduct> one = RedisUtil.get("xianshichanpin");
			if(find(sqlId,one.get(0).get("platform_id"),one.get(0).get("p_product_id"),one.get(0).get("create_time"))==null){//满标情况
				List<UnfinishedProduct> two = find(sql3,ups.get("date"));
				maps.put("xianshi", two);
				RedisUtil.set("xianshichanpin", (Serializable) two);
			}else{
				maps.put("xianshi", one);
			}
			
		}
		
		//推荐产品
		//maps.put("tuijian", find(sql4,ups.get("date")));
		if(RedisUtil.get("tuijianchanpin") == null ){ //第一遍
			List<UnfinishedProduct> one = find(sql4,ups.get("date"));
			maps.put("tuijian", one);
			RedisUtil.set("tuijianchanpin", (Serializable) one);
		}else{ 
			List<UnfinishedProduct> one = RedisUtil.get("tuijianchanpin");
			if(find(sqlId,one.get(0).get("platform_id"),one.get(0).get("p_product_id"),one.get(0).get("create_time"))==null){//满标情况
				List<UnfinishedProduct> two = find(sql4,ups.get("date"));
				maps.put("tuijian", two);
				RedisUtil.set("tuijianchanpin", (Serializable) two);
			}else{
				maps.put("tuijian", one);
			}
			
		}
		
		return maps;
	}
	
	/**
	 *按期限选标（完成度高的优先展示）
	 */
	public Map<String,List<UnfinishedProduct>> getQiXian(){
		
		Map<String,List<UnfinishedProduct>> maps = new HashMap<String, List<UnfinishedProduct>>();
		UnfinishedProduct ups =  UnfinishedProduct.dao.findFirst("select max(create_time) as date from wd_p_product_unfinished ");
		String sql1=" select * from wd_p_product_unfinished  where borrow_date < 180 and create_time = ? order by borrow_progress   desc  limit 0,3 ";//短期稳赢
		String sql2=" select * from wd_p_product_unfinished  where borrow_date >= 180 and borrow_date <= 365 and create_time = ? order by borrow_progress   desc  limit 0,3 ";//中期保收益
		String sql3=" select * from wd_p_product_unfinished  where borrow_date > 365 and create_time = ? order by borrow_progress   desc  limit 0,3 ";//长期优选
		String  sqlId = " select * from wd_p_product_unfinished  where platform_id = ? and p_product_id = ? and  create_time = ? ";
		//maps.put("duan", find(sql1,ups.get("date")));
		//短期
		if(RedisUtil.get("duanqichanpin") == null ){ //第一遍
			List<UnfinishedProduct> one = find(sql1,ups.get("date"));
			maps.put("duan", one);
			RedisUtil.set("duanqichanpin", (Serializable) one);
		}else{ 
			List<UnfinishedProduct> one = RedisUtil.get("duanqichanpin");
			List<UnfinishedProduct> c = find(sql1,ups.get("date"));
			for(int i=0;i<one.size();i++){//redis
			if(find(sqlId,one.get(i).get("platform_id"),one.get(i).get("p_product_id"),one.get(i).get("create_time"))==null){//满标情况
				 one.remove(i);
				for(int j=0;j<c.size();j++){
					if(!one.contains(c.get(j))){
						one.add(c.get(j));
						break;
					}
				}
				//one.add(i,  UnfinishedProduct.dao.findFirst("select *  from wd_p_product_unfinished  where borrow_date < 180 and create_time = ? order by borrow_progress  desc "));
			}
			}
			RedisUtil.set("duanqichanpin", (Serializable) one);
			maps.put("duan", one);
			
		}
		//中期
		//maps.put("zhong", find(sql2,ups.get("date")));
		if(RedisUtil.get("zhongqichanpin") == null ){ //第一遍
			List<UnfinishedProduct> one = find(sql2,ups.get("date"));
			maps.put("zhong", one);
			RedisUtil.set("zhongqichanpin", (Serializable) one);
		}else{ 
			List<UnfinishedProduct> one = RedisUtil.get("zhongqichanpin");
			List<UnfinishedProduct> c = find(sql2,ups.get("date"));
			for(int i=0;i<one.size();i++){//redis
			if(find(sqlId,one.get(i).get("platform_id"),one.get(i).get("p_product_id"),one.get(i).get("create_time"))==null){//满标情况
				 one.remove(i);
				for(int j=0;j<c.size();j++){
					if(!one.contains(c.get(j))){
						one.add(c.get(j));
						break;
					}
				}
				//one.add(i,  UnfinishedProduct.dao.findFirst("select *  from wd_p_product_unfinished  where borrow_date < 180 and create_time = ? order by borrow_progress  desc "));
			}
			}
			RedisUtil.set("zhongqichanpin", (Serializable) one);
			maps.put("zhong", one);

			
		}
		
		//长期
		//maps.put("chang", find(sql3,ups.get("date")));
		if(RedisUtil.get("changqichanpin") == null ){ //第一遍
			List<UnfinishedProduct> one = find(sql3,ups.get("date"));
			maps.put("chang", one);
			RedisUtil.set("changqichanpin", (Serializable) one);
		}else{ 
		
			List<UnfinishedProduct> one = RedisUtil.get("changqichanpin");
			List<UnfinishedProduct> c = find(sql3,ups.get("date"));
			for(int i=0;i<one.size();i++){//redis
			if(find(sqlId,one.get(i).get("platform_id"),one.get(i).get("p_product_id"),one.get(i).get("create_time"))==null){//满标情况
				 one.remove(i);
				for(int j=0;j<c.size();j++){
					if(!one.contains(c.get(j))){
						one.add(c.get(j));
						break;
					}
				}
				//one.add(i,  UnfinishedProduct.dao.findFirst("select *  from wd_p_product_unfinished  where borrow_date < 180 and create_time = ? order by borrow_progress  desc "));
			}
			}
			RedisUtil.set("changqichanpin", (Serializable) one);
			maps.put("chang", one);
		}
		
		return maps;
	}
	/**
	 * 大家都购买了那些产品
	 */
	public List<UnfinishedProduct> getMessageBy(){
		/**
		 select * from wd_p_product_detail a , wd_p_product_unfinished b
		 where a.p_product_id = b.p_product_id
		 */
		String sql = " select * from  wd_p_product_unfinished a, (select * from wd_p_product_detail ) b where a.p_product_id = b.p_product_id LIMIT 0,10 ";
		return find(sql);
	}
	/**
	 * 按收益选标
	 */
	public Map<String ,List<UnfinishedProduct>> getIncome(){
		Map<String,List<UnfinishedProduct>> maps = new HashMap<String, List<UnfinishedProduct>>();
		UnfinishedProduct ups =  UnfinishedProduct.dao.findFirst("select max(create_time) as date from wd_p_product_unfinished ");
		/**
		 * select * from wd_p_product_unfinished where year_rate >=16 order by year_rate desc limit 0,3 
		 *  
		 */
		String sql1 = "select * from wd_p_product_unfinished a left join wd_p_img b on a.platform_id = b.platform_id where a.year_rate >=16 and a.create_time = ? order by a.borrow_progress desc limit 0,3  ";//高收益
		String sql2 = "select * from wd_p_product_unfinished a left join wd_p_img b on a.platform_id = b.platform_id where a.year_rate >=12 and a.year_rate<16 and a.create_time = ? order by a.borrow_progress desc limit 0,3  ";//稳健
		String sql3 = "select * from wd_p_product_unfinished a left join wd_p_img b on a.platform_id = b.platform_id where a.year_rate <12 and a.create_time = ? order by a.borrow_progress desc limit 0,3  ";//低风险
		String sqlId = "select * from wd_p_product_unfinished  where platform_id = ? and p_product_id = ? and  create_time = ?";
		//高收益
		//maps.put("gao", find(sql1,ups.get("date")));
		if(RedisUtil.get("gaoshouyichanpin") == null ){ //第一遍
			List<UnfinishedProduct> one = find(sql1,ups.get("date"));
			maps.put("gao", one);
			RedisUtil.set("gaoshouyichanpin", (Serializable) one);
		}else{ 
			
			
			List<UnfinishedProduct> one = RedisUtil.get("gaoshouyichanpin");
			List<UnfinishedProduct> c = find(sql1,ups.get("date"));
			for(int i=0;i<one.size();i++){//redis
			if(find(sqlId,one.get(i).get("platform_id"),one.get(i).get("p_product_id"),one.get(i).get("create_time"))==null){//满标情况
				 one.remove(i);
				for(int j=0;j<c.size();j++){
					if(!one.contains(c.get(j))){
						one.add(c.get(j));
						break;
					}
				}
				//one.add(i,  UnfinishedProduct.dao.findFirst("select *  from wd_p_product_unfinished  where borrow_date < 180 and create_time = ? order by borrow_progress  desc "));
			}
			}
			RedisUtil.set("gaoshouyichanpin", (Serializable) one);
			maps.put("gao", one);
			
			
			
			
			
		}
		
		
		//稳健
		//maps.put("wen", find(sql2,ups.get("date")));

		
		
		
		if(RedisUtil.get("wenjianyichanpin") == null ){ //第一遍
			List<UnfinishedProduct> one = find(sql2,ups.get("date"));
			maps.put("wen", one);
			RedisUtil.set("wenjianyichanpin", (Serializable) one);
		}else{ 
			List<UnfinishedProduct> one = RedisUtil.get("wenjianyichanpin");
			List<UnfinishedProduct> c = find(sql2,ups.get("date"));
			for(int i=0;i<one.size();i++){//redis
			if(find(sqlId,one.get(i).get("platform_id"),one.get(i).get("p_product_id"),one.get(i).get("create_time"))==null){//满标情况
				 one.remove(i);
				for(int j=0;j<c.size();j++){
					if(!one.contains(c.get(j))){
						one.add(c.get(j));
						break;
					}
				}
				//one.add(i,  UnfinishedProduct.dao.findFirst("select *  from wd_p_product_unfinished  where borrow_date < 180 and create_time = ? order by borrow_progress  desc "));
			}
			}
			RedisUtil.set("wenjianyichanpin", (Serializable) one);
			maps.put("wen", one);
		}
		
		
		
		
		
		//低收益
		//maps.put("di", find(sql3,ups.get("date")));
		
		

		if(RedisUtil.get("dishouyichanpin") == null ){ //第一遍
			List<UnfinishedProduct> one = find(sql3,ups.get("date"));
			maps.put("di", one);
			RedisUtil.set("dishouyichanpin", (Serializable) one);
		}else{ 
			List<UnfinishedProduct> one = RedisUtil.get("dishouyichanpin");
			List<UnfinishedProduct> c = find(sql3,ups.get("date"));
			for(int i=0;i<one.size();i++){//redis
			if(find(sqlId,one.get(i).get("platform_id"),one.get(i).get("p_product_id"),one.get(i).get("create_time"))==null){//满标情况
				 one.remove(i);
				for(int j=0;j<c.size();j++){
					if(!one.contains(c.get(j))){
						one.add(c.get(j));
						break;
					}
				}
				//one.add(i,  UnfinishedProduct.dao.findFirst("select *  from wd_p_product_unfinished  where borrow_date < 180 and create_time = ? order by borrow_progress  desc "));
			}
			}
			RedisUtil.set("dishouyichanpin", (Serializable) one);
			maps.put("di", one);
		}
		
		
		
		return maps;
	}

}
